package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectPage;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {
    @Autowired
    private IProductMapper productMapper;

    @Override
    public void add(ProductBean product) {
        productMapper.insert(product);
    }

    @Override
    public ProductBean findById(String id) {
        return productMapper.selectById(id);
    }

    @Override
    public IPage<ProductBean> cutByItem(Integer pageNO, String name, String salePrice, String kind, Integer startNum, Integer endNum) {

        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        if (name != null && !"".equals(name)) {
            qw.like("p_name", name);
        }
        if (salePrice != null && !"".equals(salePrice)) {
            if("未定销售价".equals(salePrice)){
                qw.isNull("p_salePrice");
            }else {
                qw.isNotNull("p_salePrice");
            }
        }
            if (kind != null && !"".equals(kind)) {
            qw.like("p_kind", kind);
        }
        if (startNum != null) {
            qw.ge("p_num", startNum);
        }
        if (endNum != null) {
            qw.le("p_num", endNum);
        }

        return productMapper.selectPage(new Page<>(pageNO, 5), qw);
    }

    @Override
    public void update(String id, Integer salePrice) {
        ProductBean product = productMapper.selectById(id);
        product.setSalePrice(salePrice);
        productMapper.updateById(product);
    }


}
